package com.itheima.controller;

import com.itheima.pojo.Result;
import com.itheima.pojo.User;
import com.itheima.service.UserService;
import com.itheima.utils.ThreadLocalUtil;
import jakarta.validation.Valid;
import jakarta.validation.constraints.Pattern;
import org.hibernate.validator.constraints.URL;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@Validated
@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    UserService  userService;
    @PostMapping("/register")
    public Result register( @Pattern(regexp = "^\\S{1,16}$",message = "用户名长度为5-16位") String username,
                            @Pattern(regexp = "^\\S{5,16}$",message = "密码长度为5-16位") String password) {

        return userService.register(username, password);

    }

    /**
     * 登录接口
     */

    @PostMapping("/login")
    public Result login(String username, String password) {
        return userService.login(username, password);
    }

    /**
     * 获取用户信息
     */

    @GetMapping("/userInfo")
    public Result<User> getUserInfo() {
        return userService.getUserInfo();
    }

    /**
     * 更新用户信息
     */
    @PutMapping("/update")
    public Result updateUserInfo(@RequestBody @Valid User user) {
        return userService.updateUserInfo(user);
    }


    /**
     * 更新用户头像
     */
    @PatchMapping("/updateAvatar")
    public Result updateUserPic(@RequestParam @URL String avatarUrl) {

        userService.updateUserPic(avatarUrl);
        return Result.success();
    }

    /**
     * 更新密码
     */
    @PatchMapping("/updatePwd")
    public Result updatePwd(@RequestBody Map<String,String> map){

        return userService.updatePwd(map);

    }


}
